/**
 * 博客文章查看页面
 * @author 李华顺 <huacnlee@gmail.com>
 */
var Post = {
	/**
	 * 脚本设置
	 */
	settings: {
		
		/**
		 * 评论Ajax调用URL地址
		 */
		commentUrl : "",
		/**
		 * 当前的文章编号
		 */
		ID: -1
	},
	
	/**
	 * 脚本初始化
	 * @param {Object} settings
	 */
	init: function(settings){
		$.extend(Post.settings,settings);
		
		$("#digg-box .button a").click(function(){
			Post.digg();
			return false;
		});
		
		$("#comment-post form").bind("submit",function(){
			Post.commentPost();
			return false;
		});
		
		/* 让文本框改动的时候，去掉错误提示的样式 */
		$("#comment-post form input,#comment-post form textarea").change(function(){
			$(this).removeClass("error");
			Post.showMessage();
		});
		
		Post.bindQuoteEvent();
	},
	
	/**
	 * 绑定引用事件
	 */
	bindQuoteEvent: function(){
		$("#comment-box li a.quote").click(function(){			
			var comment_id = $(this).attr("itemid");
			Post.quote(comment_id);
			return false;
		})
	},
	
	
	/**
	 * 推荐
	 */
	digg: function(){
		var diggNum = $("#digg-box .num");	
		var diggBtn = $("#digg-box .button")
		var oldNum = parseInt(diggNum.text());
		
		diggBtn.html("...");
		$.ajax({
			url: "/user/post/digg/add/",
			type: Common.Settings.AJAX_TYPE,
			data:"id=" + Post.settings.ID,
			dataType:"json",
			success: function(state){
				if(state.success){
					diggNum.text((oldNum + 1));
					diggBtn.html("已推荐");
				}
				else{
					if (state.mark == 'twice') {
						diggBtn.html("已推荐");
					}
					else {
						diggNum.text(oldNum);
						diggBtn.html('<a href="#">推荐</a>');
					}
				}
			}
		})
	},
	
	/**
	 * 引用
	 * @param {int} id 评论编号
	 */
	quote: function(id){
		var txtBody = $("#id_body");

		if(id != undefined){
			var item = $("#comment-item-" + id);
			var name = $("a.realname",item).text();
			var body = $("div.content",item).text();			
			
			quote = QUOTE_TEMPLATE.replace("{{name}}",name).replace("{{body}}",body);
			txtBody.val(txtBody.val() + quote);
		}
		txtBody.focus();
	},
	
	/**
	 * 验证评论表单
	 */
	commentCheck: function(){
		var state = new Common.ExecuteState();
		state.success = false;
		
		var form = $("#comment-post form");
		//验证表单
		var txtName = $("#id_name");
		var txtUrl = $("#id_url");
		var txtEmail = $("#id_email");
		var txtBody = $("#id_body");
		var chkFollow = $("#id_follow");
		
		if(txtName.val() == ''){
			state.message = "还没有输入你的大名。";
			state.mark = "name";
			state.owner = txtName;
			return state;
		}
		
		url = txtUrl.val();
		if((url != '') && (url != 'http://')){
			if(! url.isUrl()){
				state.message = '博客地址输入不正确，请输入以http://开头的地址。';
				state.owner = txtUrl;
				state.mark = "url";
				return state;
			}
		}
		
		var email = txtEmail.val();
		if (email != '') {
			if (!email.isEmail()) {
				state.mark = "email";
				state.owner = txtEmail;
				state.message = "Email格式不正确。";
				return state;
			}
		}
		
		if(txtBody.val().length < 4 || txtBody.val() > 1000){
			state.message = "评论内容长度要求在4-1000字以内。";
			state.mark = "body";
			state.owner = txtBody;
			return state;
		}
		
		state.success = true;
		return state;
	},
	
	/**
	 * 发表评论
	 */
	commentPost: function(){
		var state = new Common.ExecuteState();

		state  = Post.commentCheck();
		
		if(! state.success){
			state.owner.focus();
			state.owner.addClass("error");
			Post.showMessage(state);
			return false;
		}		
		
		var txtName = $("#id_name");
		var txtUrl = $("#id_url");
		var txtEmail = $("#id_email");
		var txtBody = $("#id_body");
		var chkFollow = $("#id_follow");
		var follow = '0';
		if(chkFollow.attr("checked")){
			follow = '1';
		}
		
		

		$.ajax({
			url: '/user/post/comment/add/',
			type: Common.Settings.AJAX_TYPE,
			data: {
				id: Post.settings.ID,
				name: txtName.val(),
				body: txtBody.val(),
				follow: follow,
				url: txtUrl.val(),
				email: txtEmail.val()
			},
			dataType:"json",
			success: function(state){
				if(state.success){
					state.message = '评论发表成功。';
					Post.showMessage(state);
					txtBody.val('');
					
					var floor = $("#comment-box ul.list li").size() + 1

					//向列表中插入评论项
					var li = COMMENT_ITEM_TEMPLATE.replace("{{id}}",state.dict.id).replace("{{id}}",state.dict.id);
					li = li.replace("{{realname}}",state.dict.user.realname).replace("{{realname}}",state.dict.user.realname).replace("{{realname}}",state.dict.user.realname).replace("{{realname}}",state.dict.user.realname);
					li = li.replace("{{face}}",state.dict.user.face);
					li = li.replace("{{url}}",state.dict.user.url).replace("{{url}}",state.dict.user.url).replace("{{url}}",state.dict.user.url);
					li = li.replace("{{addtime}}",state.dict.addtime).replace("{{timesince}}",state.dict.timesince);
					li = li.replace("{{body}}",state.dict.body);
					li = li.replace("{{floor}}",floor);
					$("#comment-box ul.list").append($(li));
					
					//重新绑定引用事件
					Post.bindQuoteEvent();
				}
				else{
					Post.showMessage(state);
				}
			}
		})
	},
	
	
	/**
	 * 显示消息提示
	 * @param {Object} state ExecuteState 对象
	 */
	showMessage : function(state){

		var messageBox = $("#messageBox");
		if(state && state != undefined){

			messageBox.fadeOut();
			messageBox.text(state.message);
			if (state.success) {
				messageBox.css("color","green");
			}
			else {
				messageBox.css("color","red");
			}

			messageBox.fadeIn();

		}
		else{
			messageBox.text("");
		}

	}
}


QUOTE_TEMPLATE = "引用 [{{name}}]：\n{{body}}\n------------------------------------------------------\n\n\n";
COMMENT_ITEM_TEMPLATE = '\
<li id="comment-item-{{id}}">\
	<div class="top">\
		<span class="face">\
			<a title="{{realname}}" href="{{url}}">\
				<img alt="{{realname}}头像" src="{{face}}?width=24&amp;height=24&amp;mode=flickr"/>\
			</a>\
		</span>\
		<a class="realname" title="{{realname}}" href="{{url}}">{{realname}}</a>\
		<span title="{{timesince}}前" class="date">{{addtime}}</span>\
	</div>\
	<div class="tool">\
		<span>第<span class="floor">{{floor}}</span>楼</span><a class="quote" title="引用这条评论" itemid="{{id}}" href="#">引用</a>\
	</div>\
	<div class="content">{{body}}</div>\
</li>\
';
